package com.soulkun.smarthome.ui.main.personal.dialog

import android.view.View
import android.view.ViewGroup
import androidx.databinding.ObservableField
import androidx.fragment.app.viewModels
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppDialogMainPersonalFamilyRoomAddBinding
import com.soulkun.smarthome.logic.vo.RoomVO
import com.soulkun.smarthome.ui.main.personal.viewmodel.*
import soulkun.library.mvvm.component.BaseDataBindingDialogFragment
import soulkun.library.utils.toPx
import soulkun.library.utils.toast

class AppMainPersonalFamilyRoomAddDialog: BaseDataBindingDialogFragment<AppDialogMainPersonalFamilyRoomAddBinding>() {

    private val roomViewModel by lazy {
        requireParentFragment().viewModels<AppMainPersonalFamilySettingRoomViewModel> { defaultViewModelProviderFactory }.value
    }
    private val viewModel by viewModels<AppMainPersonalFamilyRoomAddViewModel> { defaultViewModelProviderFactory }

    override fun setLayout(): Int {
        return R.layout.app_dialog_main_personal_family_room_add
    }

    override fun doOnViewCreated() {
        requireDialog().window?.run {
            setLayout(requireContext().resources.getDimensionPixelSize(R.dimen.sw_300dp), ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        isCancelable = false
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.iv_main_personal_family_room_add_icon_delete -> {
                    viewModel.roomNameEditLiveData.value = ""
                }
                R.id.tv_main_personal_family_room_add_text_cancel -> {
                    dismiss()
                }
                R.id.tv_main_personal_family_room_add_text_determine -> {
                    roomViewModel.addRoom(viewModel.roomNameEditLiveData.value!!) { response ->
                        response.statusMessage.toast()
                        dismiss()
                    }
                }
            }
        }
    }

    override fun doOnResume() {
        requireDataBinding().etMainPersonalFamilyRoomAddTextRoomName.run {
            requestFocus()
//            val inputMethodManager =
//                requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
//            inputMethodManager.toggleSoftInput(
//                0,
//                InputMethodManager.HIDE_NOT_ALWAYS
//            )
        }
    }

    override fun doOnDismiss() {
    }

    override fun doOnDestroyView() {
    }

}